package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.OrderDetailAdapter;
import com.zcx.qshop.conn.JsonAddressAsyGet;
import com.zcx.qshop.conn.JsonCouponAsyGet;
import com.zcx.qshop.conn.JsonOrderAsyGet;
import com.zcx.qshop.conn.JsonOrderinfoAsyGet;
import com.zcx.qshop.conn.JsonOrderupdateaddressAsyGet;
import com.zcx.qshop.conn.JsonOrderupdatecouponAsyGet;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.view.CouponView;
import com.zcx.qshop.view.TitleView;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public class OrderDetailActivity extends QSActivity implements View.OnClickListener{

    public static OnOrderChangeCallBack onOrderChangeCallBack;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.order_detail_time) private TextView time;

    @BoundView(R.id.order_detail_state) private TextView state;

    @BoundView(R.id.order_detail_list_view) private ListView listView;

    @BoundView(R.id.order_detail_address) private View address;

    @BoundView(R.id.order_detail_username) private TextView username;

    @BoundView(R.id.order_detail_mobile) private TextView mobile;

    @BoundView(R.id.order_detail_location) private TextView location;

    @BoundView(R.id.order_detail_coupon) private ViewGroup coupon;

    @BoundView(R.id.order_detail_coupon_view) private CouponView couponView;

    @BoundView(R.id.order_detail_number) private TextView number;

    @BoundView(R.id.order_detail_acount) private TextView acount;

    private JsonOrderAsyGet.Info.Order order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_order_detail);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("确认订单");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        order = (JsonOrderAsyGet.Info.Order)getIntent().getSerializableExtra("Order");

        new JsonOrderinfoAsyGet(order.id, new AsyCallBack<JsonOrderinfoAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonOrderinfoAsyGet.Info info) throws Exception {

                listView.addHeaderView(BoundViewHelper.boundView(OrderDetailActivity.this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.header_order_detail, null))));

                listView.addFooterView(BoundViewHelper.boundView(OrderDetailActivity.this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.footer_order_detail, null))));

                listView.setAdapter(new OrderDetailAdapter(OrderDetailActivity.this, info.goods));

                time.setText(info.posttime);

                state.setText(info.type);

                username.setText(info.name);

                mobile.setText(info.mobile);

                location.setText(info.address);

                number.setText("共" + info.number + "件    合计：");

                ((TextView)coupon.getChildAt(1)).setText(info.cnumber + "张可用");

                acount.setText(info.acount);

                setCoupon(info.coupon);

                onOrderChangeCallBack = new OnOrderChangeCallBack() {

                    @Override
                    public void onAddressChange(JsonAddressAsyGet.Info.Address address) {

                        new JsonOrderupdateaddressAsyGet(order.id, address.aid, new AsyCallBack<JsonOrderupdateaddressAsyGet.Info>(){

                            @Override
                            public void onSuccess(String toast, int type, JsonOrderupdateaddressAsyGet.Info info) throws Exception {

                                username.setText(info.name);

                                mobile.setText(info.mobile);

                                location.setText(info.address);

                            }

                        }).execute(OrderDetailActivity.this);

                    }

                    @Override
                    public void onCouponChange(final JsonCouponAsyGet.Info.Coupon coupon) {

                        new JsonOrderupdatecouponAsyGet(order.id, coupon.cid, new AsyCallBack<String>(){

                            @Override
                            public void onSuccess(String toast, int type, String s) throws Exception {

                                setCoupon(coupon);

                                acount.setText(s);

                            }

                        }).execute(OrderDetailActivity.this);

                    }

                };

                if(info.type.equals("未付款")){

                    address.setOnClickListener(OrderDetailActivity.this);

                    coupon.setOnClickListener(OrderDetailActivity.this);

                    couponView.setOnClickListener(OrderDetailActivity.this);

                }

            }

        }).execute(this);

    }

    private void setCoupon(JsonCouponAsyGet.Info.Coupon coupons){

        if(coupons != null){

            coupon.setVisibility(View.GONE);

            couponView.setLoad(coupons.price, coupons.fullprice, coupons.endtime, true);

        }else{

            coupon.setVisibility(View.VISIBLE);

            couponView.setVisibility(View.GONE);

        }

    }

    @Override
    public void onClick(final View v) {

        switch (v.getId()){

            case R.id.order_detail_address:

                startVerifyActivity(AddressManageActivity.class);

                break;

            case R.id.order_detail_coupon_view:

                new WhetherDialog(this, "取消优惠券"){

                    @Override
                    protected void onYes() {

                        new JsonOrderupdatecouponAsyGet(order.id, "", new AsyCallBack<String>(){

                            @Override
                            public void onSuccess(String toast, int type, String s) throws Exception {

                                setCoupon(null);

                                acount.setText(s);

                            }

                        }).execute(OrderDetailActivity.this);

                    }

                }.show();

                break;
            case R.id.order_detail_coupon:

                startVerifyActivity(CouponActivity.class, new Intent().putExtra("totalprice", acount.getText().toString()));

                break;

        }

    }

    @Override
    protected void onDestroy() {

        onOrderChangeCallBack = null;

        super.onDestroy();

    }

    public interface OnOrderChangeCallBack{

        void onAddressChange(JsonAddressAsyGet.Info.Address address);

        void onCouponChange(JsonCouponAsyGet.Info.Coupon coupon);

    }

}
